package com.example.shop_2211.search

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.example.base.entity.GoodsBean
import com.example.base.entity.GoodsEntityResult
import com.example.base.mvvm.BaseViewModel
import kotlinx.coroutines.launch

class SearchNewViewModel:BaseViewModel() {
    val success = MutableLiveData<GoodsEntityResult>()
    val fail = MutableLiveData<String>()
    fun searchNew(keyword:String,limit:Int,page:Int){
        viewModelScope.launch {
            try {
                success.value = apiService.searchNew(keyword, limit, page)
            }catch (e:Exception){
                fail.value = e.message
            }
        }
    }
    var newSuccess = MutableLiveData<List<GoodsBean>>()
    val newFail = MutableLiveData<String>()
    fun newSearch(keyword:String){
        viewModelScope.launch {
            try {
                val newSearch = apiService.newSearch(100, 1)//查询出所有数据
                val list = newSearch.data.list.filter {
                    it.storeName.contains(keyword)
                }
                newSuccess.value = list
            }catch (e:Exception){
                newFail.value = e.message
            }
        }
    }
}